package com.myq.note.service.impl;

import com.myq.note.dao.NoteDao;
import com.myq.note.po.Note;
import com.myq.note.po.Result;
import com.myq.note.service.NoteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class NoteServiceImpl implements NoteService {

    @Autowired
    public NoteDao noteDao;

    @Override
    public Result findAllNote(Note note) {

        Result<List<Note>> result = new Result<>();
        List<Note> list;
        if (note.getUser_id() == null) {

            list = noteDao.findBookNote(note);
        }else {

            list = noteDao.findUserNote(note);

        }
        //System.out.println(list);
        result.setStatus(200);
        result.setMes("查询成功");
        result.setData(list);
        return result;
    }

    @Override
    public Result findById(String note_id) {
        //System.out.println(note_id);
        Result<Note> result = new Result<>();
        Note note = noteDao.findById(note_id);
        //System.out.println(note);
        //System.out.println(list);
        result.setStatus(200);
        result.setMes("查询成功");
        result.setData(note);
        return result;
    }

    @Override
    public Result createNote(Note note) {

        Result<Note> result = new Result<>();
        String uuid = UUID.randomUUID().toString();
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = simpleDateFormat.format(date);

        note.setNote_id(uuid);
        note.setNote_createtime(format);
        note.setNote_lastmodify(format);
        note.setNote_status("1");
        int note1 = noteDao.createNote(note);
        result.setData(note);
        result.setStatus(200);


        return result;
    }

    @Override
    public Result updateNote(Note note) {
        Result<Note> result = new Result<>();
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = simpleDateFormat.format(date);
        note.setNote_lastmodify(format);

        noteDao.updateNote(note);
        result.setStatus(200);

        return result;
    }

    @Override
    public Result deleteNote(Note note) {
        noteDao.deleteNote(note);
        Result result = new Result<>();
        result.setStatus(200);

        return result;
    }

    @Override
    public Result deleteSureNote(Note note) {

        noteDao.deleteSureNote(note);
        Result result = new Result();
        result.setStatus(200);


        return result;
    }
}
